package com.qdxwx.web.controller.teacher;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.teacher.TeacherQuery;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Teacher接口
 * @author developer
 */
@Controller
@RequestMapping("/teacher")
public class TeacherListController extends BaseController {

	@Autowired
	private TeacherService teacherService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private BookService bookService;
	@Autowired
	private SubjectService subjectService;

	/**
	 * 教师列表展示
	 */
	@RequestMapping({"/index.html", "/list.html"})
	public String teacherList(HttpServletRequest request, ModelMap model) {

		if (TKRequest.isMobileBrowse(request)){
			return getViewPathH5("teacher/teacher-list");
		}

		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		int subjectId = TKConvert.toInteger(request.getParameter("subjectId"));
		int starId = TKConvert.toInteger(request.getParameter("starId"));
		int isElite = TKConvert.toInteger(request.getParameter("isElite"));
		int isHot = TKConvert.toInteger(request.getParameter("isHot"));
		int orderBy = TKConvert.toInteger(request.getParameter("orderBy"));
		//String searchKey = TKString.searchKeyFilter(request.getParameter("searchKey"));
		TeacherQuery query = new TeacherQuery();
		//query.setName(searchKey);
		query.setSubjectId(subjectId);
		query.setStarId(starId);
		query.setIsElite(isElite);
		query.setIsHot(isHot);
		query.setOrderBy(orderBy);
		query.setIsDisabled(0);
		PageRequest page = new PageRequest();
		page.setCurrentPage(currentPage);
		page.setPageSize(8);
		model.put("totalPages", page.getTotalPages(teacherService.count(query)));
		// 查询老师
		List<Teacher> teacherList = teacherService.getList(query, page);
		model.put("teacherList", teacherList);
		model.put("subjects", subjectService.getList());
		model.put("query", query);
		model.put("page", page);
		return getViewPath("teacher/teacher-list");
	}

	@PostMapping("/get/list.json")
	@ResponseBody
	public AjaxResult list(HttpServletRequest request){
		int teacherId = TKConvert.toInteger(request.getParameter("teacherId"));
		String type = request.getParameter("type");
		if(teacherId < 1 || TKString.isEmpty(type)) return AjaxResult.error("参数异常");
		int page = TKConvert.toInteger(request.getParameter("page"),1);
		PageRequest pageRequest = new PageRequest();
		pageRequest.setCurrentPage(page);
		Map<String,Object> data = new HashMap<>();
		switch (type){
			case "course":
				pageRequest.setPageSize(6);
				CourseQuery courseQuery = new CourseQuery();
				courseQuery.setChannelId(2);
				courseQuery.setIsAvailable(1);
				courseQuery.setTeacherId(teacherId);
				data.put("data",courseService.getList(courseQuery,pageRequest));
				data.put("page",pageRequest);
				return AjaxResult.success(data);
			case "pro":
				pageRequest.setPageSize(8);
				CourseQuery query = new CourseQuery();
				query.setChannelId(8);
				query.setIsAvailable(1);
				query.setTeacherId(teacherId);
				data.put("data",courseService.getList(query,pageRequest));
				data.put("page",pageRequest);
				return AjaxResult.success(data);
			case "book":
				pageRequest.setPageSize(6);
				data.put("data",bookService.getListByTeacherId(teacherId,pageRequest));
				data.put("page",pageRequest);
				return AjaxResult.success(data);
			default:
				return AjaxResult.error("参数异常");
		}
	}
}